import { defineEventHandler, getQuery } from 'h3';

export default defineEventHandler(async (event) => {
  const query = getQuery(event);
  const { pageNo = 1, pageSize = 10, type, category, keyword } = query;

  // 能力定义数据
  const capabilityDefinitions = [
    {
      id: 1,
      code: 'ROOM_MANAGEMENT',
      name: '客房管理能力',
      description: '提供客房状态管理、房态更新、房间分配等核心能力',
      icon: 'ant-design:home-outlined',
      color: '#52c41a',
      type: 'business',
      category: 'hotel',
      version: '1.0.0',
      tags: ['客房管理', '房态管理'],
      implementation_type: 'service',
      implementation_config: {
        serviceName: 'roomManagementService',
        methodName: 'updateRoomStatus',
        parameters: {
          statusTypes: [
            'vacant',
            'occupied',
            'reserved',
            'maintenance',
            'dirty',
          ],
          enableRealTimeSync: true,
          autoStatusTransition: true,
        },
      },
      is_active: true,
      created_by: 'admin',
      created_time: '2024-01-01 00:00:00',
      updated_by: 'admin',
      updated_time: '2024-01-01 00:00:00',
      deleted: 0,
    },
    {
      id: 2,
      code: 'BOOKING_MANAGEMENT',
      name: '预订管理能力',
      description: '提供客房预订、订单处理、预订确认等业务能力',
      icon: 'ant-design:calendar-outlined',
      color: '#1890ff',
      type: 'business',
      category: 'hotel',
      version: '1.0.0',
      tags: ['预订管理', '订单处理'],
      implementation_type: 'service',
      implementation_config: {
        serviceName: 'bookingManagementService',
        methodName: 'processBooking',
        parameters: {
          bookingTypes: ['individual', 'group', 'corporate'],
          paymentMethods: ['cash', 'card', 'online', 'voucher'],
          enableOverbooking: true,
          maxOverbookingRate: 0.1,
        },
      },
      is_active: true,
      created_by: 'admin',
      created_time: '2024-01-01 00:00:00',
      updated_by: 'admin',
      updated_time: '2024-01-01 00:00:00',
      deleted: 0,
    },
    {
      id: 3,
      code: 'CHECKIN_CHECKOUT',
      name: '入住退房能力',
      description: '提供客人入住登记、退房结算、房卡管理等能力',
      icon: 'ant-design:user-add-outlined',
      color: '#722ed1',
      type: 'business',
      category: 'hotel',
      version: '1.0.0',
      tags: ['入住管理', '退房管理'],
      implementation_type: 'workflow',
      implementation_config: {
        processDefinitionId: 'checkin_checkout_process',
        version: '1.0.0',
        parameters: {
          enableAutoCheckout: true,
          lateCheckoutAllowed: true,
          maxLateCheckoutHours: 2,
          enableExpressCheckout: true,
        },
      },
      is_active: true,
      created_by: 'admin',
      created_time: '2024-01-01 00:00:00',
      updated_by: 'admin',
      updated_time: '2024-01-01 00:00:00',
      deleted: 0,
    },
    {
      id: 4,
      code: 'PRICING_ENGINE',
      name: '定价引擎能力',
      description: '提供动态定价、价格策略、优惠计算等能力',
      icon: 'ant-design:dollar-outlined',
      color: '#fa8c16',
      type: 'business',
      category: 'hotel',
      version: '1.0.0',
      tags: ['定价策略', '价格管理'],
      implementation_type: 'rule',
      implementation_config: {
        ruleSetName: 'hotel_pricing_rules',
        engine: 'drools',
        parameters: {
          pricingFactors: ['season', 'occupancy', 'competition', 'demand'],
          enableDynamicPricing: true,
          updateFrequency: 'hourly',
          enableAIBasedPricing: true,
        },
      },
      is_active: true,
      created_by: 'admin',
      created_time: '2024-01-01 00:00:00',
      updated_by: 'admin',
      updated_time: '2024-01-01 00:00:00',
      deleted: 0,
    },
    {
      id: 5,
      code: 'GUEST_SERVICE',
      name: '客人服务能力',
      description: '提供客人服务请求、客房服务、投诉处理等能力',
      icon: 'ant-design:customer-service-outlined',
      color: '#eb2f96',
      type: 'business',
      category: 'hotel',
      version: '1.0.0',
      tags: ['客人服务', '投诉处理'],
      implementation_type: 'service',
      implementation_config: {
        serviceName: 'guestServiceService',
        methodName: 'handleServiceRequest',
        parameters: {
          serviceTypes: [
            'room_service',
            'housekeeping',
            'maintenance',
            'concierge',
          ],
          priorityLevels: ['low', 'medium', 'high', 'urgent'],
          enableAutoAssignment: true,
        },
      },
      is_active: true,
      created_by: 'admin',
      created_time: '2024-01-01 00:00:00',
      updated_by: 'admin',
      updated_time: '2024-01-01 00:00:00',
      deleted: 0,
    },
    {
      id: 6,
      code: 'HOUSEKEEPING_MANAGEMENT',
      name: '客房清洁能力',
      description: '提供客房清洁计划、清洁状态跟踪、清洁质量检查等能力',
      icon: 'ant-design:clean-outlined',
      color: '#13c2c2',
      type: 'business',
      category: 'hotel',
      version: '1.0.0',
      tags: ['客房清洁', '质量检查'],
      implementation_type: 'workflow',
      implementation_config: {
        processDefinitionId: 'housekeeping_process',
        version: '1.0.0',
        parameters: {
          cleaningSchedule: 'daily',
          qualityCheckRequired: true,
          enableRealTimeTracking: true,
        },
      },
      is_active: true,
      created_by: 'admin',
      created_time: '2024-01-01 00:00:00',
      updated_by: 'admin',
      updated_time: '2024-01-01 00:00:00',
      deleted: 0,
    },
    {
      id: 7,
      code: 'PMS_INTEGRATION',
      name: 'PMS系统集成能力',
      description: '提供与酒店PMS系统的数据同步和业务集成能力',
      icon: 'ant-design:api-outlined',
      color: '#1890ff',
      type: 'technical',
      category: 'integration',
      version: '1.0.0',
      tags: ['系统集成', '数据同步'],
      implementation_type: 'api',
      implementation_config: {
        apiEndpoint: '/api/pms/integration',
        method: 'POST',
        parameters: {
          syncInterval: '5min',
          enableRealTimeSync: true,
          dataTypes: ['booking', 'room_status', 'guest_info'],
        },
      },
      is_active: true,
      created_by: 'admin',
      created_time: '2024-01-01 00:00:00',
      updated_by: 'admin',
      updated_time: '2024-01-01 00:00:00',
      deleted: 0,
    },
    {
      id: 8,
      code: 'PAYMENT_GATEWAY',
      name: '支付网关能力',
      description: '提供多种支付方式集成和支付处理能力',
      icon: 'ant-design:credit-card-outlined',
      color: '#52c41a',
      type: 'technical',
      category: 'payment',
      version: '1.0.0',
      tags: ['支付处理', '网关集成'],
      implementation_type: 'service',
      implementation_config: {
        serviceName: 'paymentGatewayService',
        methodName: 'processPayment',
        parameters: {
          paymentMethods: [
            'alipay',
            'wechat',
            'unionpay',
            'visa',
            'mastercard',
          ],
          enableRefund: true,
          enablePartialRefund: true,
        },
      },
      is_active: true,
      created_by: 'admin',
      created_time: '2024-01-01 00:00:00',
      updated_by: 'admin',
      updated_time: '2024-01-01 00:00:00',
      deleted: 0,
    },
    {
      id: 9,
      code: 'IOT_DEVICE_MANAGEMENT',
      name: 'IoT设备管理能力',
      description: '提供智能门锁、温控器、照明等IoT设备管理能力',
      icon: 'ant-design:setting-outlined',
      color: '#722ed1',
      type: 'technical',
      category: 'iot',
      version: '1.0.0',
      tags: ['IoT设备', '智能控制'],
      implementation_type: 'service',
      implementation_config: {
        serviceName: 'iotDeviceService',
        methodName: 'controlDevice',
        parameters: {
          deviceTypes: [
            'smart_lock',
            'thermostat',
            'lighting',
            'tv',
            'curtain',
          ],
          enableRemoteControl: true,
          enableAutomation: true,
        },
      },
      is_active: true,
      created_by: 'admin',
      created_time: '2024-01-01 00:00:00',
      updated_by: 'admin',
      updated_time: '2024-01-01 00:00:00',
      deleted: 0,
    },
    {
      id: 10,
      code: 'REAL_TIME_COMMUNICATION',
      name: '实时通信能力',
      description: '提供客房服务、前台沟通等实时消息通信能力',
      icon: 'ant-design:message-outlined',
      color: '#fa8c16',
      type: 'technical',
      category: 'communication',
      version: '1.0.0',
      tags: ['实时通信', '消息推送'],
      implementation_type: 'service',
      implementation_config: {
        serviceName: 'communicationService',
        methodName: 'sendMessage',
        parameters: {
          messageTypes: ['notification', 'alert', 'reminder'],
          enablePushNotification: true,
          enableSMS: true,
        },
      },
      is_active: true,
      created_by: 'admin',
      created_time: '2024-01-01 00:00:00',
      updated_by: 'admin',
      updated_time: '2024-01-01 00:00:00',
      deleted: 0,
    },
    {
      id: 11,
      code: 'DATA_ANALYTICS',
      name: '数据分析能力',
      description: '提供酒店运营数据分析和商业智能能力',
      icon: 'ant-design:bar-chart-outlined',
      color: '#eb2f96',
      type: 'technical',
      category: 'analytics',
      version: '1.0.0',
      tags: ['数据分析', '商业智能'],
      implementation_type: 'service',
      implementation_config: {
        serviceName: 'analyticsService',
        methodName: 'analyzeData',
        parameters: {
          analysisTypes: [
            'occupancy',
            'revenue',
            'guest_satisfaction',
            'operational_efficiency',
          ],
          enablePredictiveAnalytics: true,
          enableRealTimeDashboards: true,
        },
      },
      is_active: true,
      created_by: 'admin',
      created_time: '2024-01-01 00:00:00',
      updated_by: 'admin',
      updated_time: '2024-01-01 00:00:00',
      deleted: 0,
    },
    {
      id: 12,
      code: 'SECURITY_MONITORING',
      name: '安全监控能力',
      description: '提供酒店安全监控、门禁管理、访客登记等能力',
      icon: 'ant-design:safety-outlined',
      color: '#ff4d4f',
      type: 'technical',
      category: 'security',
      version: '1.0.0',
      tags: ['安全监控', '门禁管理'],
      implementation_type: 'service',
      implementation_config: {
        serviceName: 'securityService',
        methodName: 'monitorSecurity',
        parameters: {
          monitoringTypes: [
            'cctv',
            'access_control',
            'fire_alarm',
            'emergency_exit',
          ],
          enableFacialRecognition: true,
          enableVisitorManagement: true,
        },
      },
      is_active: true,
      created_by: 'admin',
      created_time: '2024-01-01 00:00:00',
      updated_by: 'admin',
      updated_time: '2024-01-01 00:00:00',
      deleted: 0,
    },
  ];

  // 过滤数据
  let filteredData = capabilityDefinitions.filter((item) => item.deleted === 0);

  if (type) {
    filteredData = filteredData.filter((item) => item.type === type);
  }

  if (category) {
    filteredData = filteredData.filter((item) => item.category === category);
  }

  if (keyword) {
    filteredData = filteredData.filter(
      (item) =>
        item.name.includes(keyword as string) ||
        item.code.includes(keyword as string) ||
        item.description.includes(keyword as string),
    );
  }

  // 分页
  const startIndex = (Number(pageNo) - 1) * Number(pageSize);
  const endIndex = startIndex + Number(pageSize);
  const paginatedData = filteredData.slice(startIndex, endIndex);

  return {
    code: 0,
    message: '查询成功',
    data: {
      list: paginatedData,
      total: filteredData.length,
      pageNo: Number(pageNo),
      pageSize: Number(pageSize),
    },
  };
});
